

def bubble_sort(arr):
    n = len(arr)

    for j in range(0, n-1):
        # 最后一轮比较为arr[0]和arr[1]的比较
        # range(x) x 最小为1
        # n - 1 - j = 1 => j = n -2 (j最大可以取得值)
        for i in range(n-1-j):
            if arr[i] > arr[i+1]:
                arr[i], arr[i+1] = arr[i+1], arr[i]


if __name__ == '__main__':
    arr = [3, 1, 2, 5, 4]
    bubble_sort(arr)
    assert arr == [1, 2, 3, 4, 5]